<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_SESSION['merchid']){
	$merchid=$this->checklogin();
	unset($_SESSION['merchid']);
}else{
	$merchid=pdo_getcolumn('jy_supplier_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
	if(!$merchid){
		exi('您还未注册店铺','error',custom_url('jy_supplier/register/',true));
		$info=pdo_get('core_members',array('id'=>$_SESSION['uid']),array('mobile','nickname','realname'));
		pdo_insert('jy_supplier_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1,'jointime'=>time(),'realname'=>$info['realname'],'mobile'=>$info['mobile'],'merchname'=>$info['nickname']));
		$merchid=pdo_insertid();
	}
}
$this->model('pei')->updayorder();
$_SESSION['merchid']=$merchid;
$return['item']=pdo_get('jy_supplier_user',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
$return['item']['orderfee']=pdo_getsum('jy_weishop_order',array('api_id'=>$merchid,'api_pluginname'=>'jy_supplier','uniacid'=>$_SESSION['uniacid']),'price');
$return['item']['ordernum']=pdo_count('jy_weishop_order',array('api_id'=>$merchid,'api_pluginname'=>'jy_supplier','uniacid'=>$_SESSION['uniacid']),'id');
$return['item']['goodsnum']=pdo_count('jy_weishop_goods',array('api_id'=>$merchid,'api_pluginname'=>'jy_supplier','uniacid'=>$_SESSION['uniacid']),'id');
$avatar=pdo_getcolumn('core_members',array('id'=>$return['item']['mid']),'avatar');
$return['item']['logo']=$return['item']['logo']?$return['item']['logo']:$avatar;
$return['status1']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'api_id'=>$merchid,'api_pluginname'=>'jy_supplier','status'=>1));
$return['status2']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'api_id'=>$merchid,'api_pluginname'=>'jy_supplier','status'=>2));
$return['ispay']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'api_id'=>$merchid,'api_pluginname'=>'jy_supplier','status'=>0,'ispay'=>0));
$return['goods']=pdo_count('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'api_id'=>$merchid,'api_pluginname'=>'jy_supplier'));
//计算待结算金额
$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>3);
$ids=pdo_getall('jy_weishop_order',$params,'id','id');
$params=array('uniacid'=>$_SESSION['uniacid'],'api_id'=>$merchid,'api_pluginname'=>'jy_supplier','clearstatus'=>0);
if($ids){
		$ids=array_keys($ids);
		$params['orderid']=$ids;
}
$order_goods=pdo_getall('jy_weishop_order_goods',$params,array('id','orderid','api_id','api_pluginname','goodsid','optionid','fee_desc'));
$clearfee0=0;
$commission_money0=0;
foreach($order_goods as &$gv){
			$order=pdo_get('jy_weishop_order',array('id'=>$gv['orderid']),array('ordersn','createtime'));
			$gv['ordersn']=$order['ordersn'];
			$gv['createtime']=$order['createtime'];
			$gv['ordergoodsid']=$gv['id'];
			$gv['id']=$gv['orderid'];
			//商品规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$optionid),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
			}
			if(!$gv['fee_desc']){
						model('jy_weishop/order')->setfee_desc($gv['orderid']);
						$gv['total_price']=pdo_getcolumn('jy_weishop_order_goods',array('id'=>$gv['ordergoodsid']),'total_price');
						$clearfee0 +=$gv['total_price'];
			}
			//计算佣金
			if($this->plugin_exists('jy_commission')){
					$gv['commission_money']=model('jy_commission/commission')->get_goodsfee($gv['goodsid'],$gv['orderid'],$gv['ordergoodsid']);
					$commission_money0 +=$gv['commission_money'];
			}
}
$return['clearfee0']=price($clearfee0);
$return['commission_money0']=price($commission_money0);
$return['signurl']=custom_url('jy_supplier/signlog/up/',true);
exi($return);